import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib

matplotlib.rc("font", family='Source Han Sans CN', weight="regular", size="10")

file_path = "csv/911.csv"
df = pd.read_csv(file_path)

title_df = df["title"]
title_series = title_df.str.split(":")
title_list = list(set([i[0] for i in title_series]))

# 构造零全0数组统计数量
zeros = pd.DataFrame(np.zeros((df.shape[0], len(title_list))), columns=title_list)
# for i in range(df.shape[0]):
#     zeros.loc[i, title_series[i][0]] = 1

# 上面的代码要遍历40w+条，而下面的只用遍历3次
for i in title_list:
    zeros[i][title_df.str.contains(i)] = 1

plt.xlabel("类型")
plt.ylabel("数量")
plt.title("911电话报警类型统计图", weight="bold", size="16")
plt.xticks(range(3), title_list)

plt.bar(range(3), [zeros["EMS"].sum(), zeros["Fire"].sum(), zeros["Traffic"].sum()])
plt.show()
